import Vue from 'vue'
import VueRouter from 'vue-router'

import home from './modules/home'
import device from './modules/device'

import system from './modules/system'
import customer from './modules/customer'

Vue.use(VueRouter);

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
	coll: true                   用于左侧菜单折叠时文字不隐藏的情况，只有children.length > 1的顶级菜单才设置
    icon: 'svg-name'             the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
    keepAlive: true;             set  'true' -- 用来判断该路由显示的界面是否是需要缓存--
  }
 */

const routes = [
	{
	    path: '/login',
	    component: () => import('@/views/login/index'),
		hidden: true,
		meta: {
			title: '登录'
		}
	},{
		path: '/404',
		component: () => import('@/views/error/404'),
		hidden: true,
		meta: {
			title: '404'
		}
	},
	home,
	//设备管理
	device,
	//客户管理
	customer,
	//系统管理
	system,
	//404页面必须放在最后
	// {
	// 	path: '*',
	// 	redirect: '/404',
	// 	hidden: true
	// }
];

const createRouter = () => new VueRouter({
    // mode: 'history',  //如果打包为二级页面，类似于 /web/ 的类型，就要设置为hash
    mode: 'hash',
    base: process.env.BASE_URL,
    routes
});

const router = createRouter();

/*
 * 重写路由的push方法
 * 屏蔽Navigating to current location (XXX) is not allowed错误
 */
const routerPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return routerPush.call(this, location).catch(error => error);
};

//重置路由，用于退出登录或者重新登录
export function resetRouter() {
	const newRouter = createRouter();
	router.matcher = newRouter.matcher;
}

export default router
